/*
 * StartupFrame.java
 * Team qq 2011
 */
package com.google.code.timetrail.gui;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.google.code.timetrail.backend.*;
import com.google.code.timetrail.presenter.StartupFrameBackend;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

public class StartupFrame extends JPanel{

    /**
     * 
     */
    private static final long serialVersionUID = 3571350411999037117L;
    private Control myGameControl;
    private JLabel gameTitleLabel;
    private JLabel enterNameLabel;
    private JTextField enterNameTextField;
    private JLabel addMembersLabel;
    private JList additionalMembersList;
    private JButton addButton;
    protected JFrame frame;
    protected List<String> additionalPlayers;
    private JList professionList;
    private JLabel professionTextField;
    private JButton nextButton;
    private StartupFrameBackend startupBackend;
    private JPanel myCD;
    public StartupFrame(Control gameControl, JPanel cd){
        this.myGameControl = gameControl;
        this.myCD = cd;
        startupBackend = new StartupFrameBackend(this.myGameControl);
        
        gameTitleLabel = new JLabel(startupBackend.getTitleLabel());
        
        enterNameLabel = new JLabel("Name:");
        
        enterNameTextField = new JTextField();
        enterNameTextField.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                
            }
        });
        enterNameTextField.setText("Player1");
        enterNameTextField.setColumns(10);
        
        addMembersLabel = new JLabel("Additional Party members:");
        
        final DefaultListModel model = new DefaultListModel();
        additionalMembersList = new JList(model);
        additionalMembersList.setVisibleRowCount(4);
        
        addButton = new JButton(startupBackend.getAddButtonText());
        addButton.addActionListener(new ActionListener() {
            
            
            

            public void actionPerformed(ActionEvent e) {
                startupBackend.pressAddButton();
                if(startupBackend.canAddPlayer()){
                    model.add(additionalMembersList.getModel().getSize(), startupBackend.getAddedPlayer());
                }

            }
        });
        
        professionList = new JList();
        professionList.addListSelectionListener(new ListSelectionListener() {


            public void valueChanged(ListSelectionEvent e) {
                startupBackend.professionValueChanged(professionList.getSelectedIndex());
                
            }
        });
        professionList.setModel(new AbstractListModel() {
            /**
             * 
             */
            private static final long serialVersionUID = 7908744601807059465L;
            String[] values = startupBackend.getProfessionValues();
            public int getSize() {
                return values.length;
            }
            public Object getElementAt(int index) {
                return values[index];
            }
        });
        professionList.setVisibleRowCount(4);
        
        professionTextField = new JLabel(startupBackend.getProfessionLabelText());
        
        nextButton = new JButton(startupBackend.getNextButtonText());
        nextButton.addActionListener(new ActionListener() {


            public void actionPerformed(ActionEvent e) {
                //CardLayout cd = (CardLayout) contentPane.getLayout();
                

                startupBackend.setNameText(enterNameTextField.getText());
                if(startupBackend.canPressNextButton()){
                myCD.add(new RationsPaceFrame(myGameControl, myCD, "Startup"), "name_1317840867491837000");
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.last(myCD);
                }
                    //cd.next(contentPane);
                    //selectedProfession - the String selected profession
                    //name = player name, can't be empty or null
                    //additionalPlayers - array that holds the additional players, null if empty
                    
                
            }
        });
        GroupLayout groupLayout = new GroupLayout(this);
        groupLayout.setHorizontalGroup(
        	groupLayout.createParallelGroup(Alignment.TRAILING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addContainerGap(477, Short.MAX_VALUE)
        			.addComponent(nextButton, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
        			.addContainerGap())
        		.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
        			.addGap(265)
        			.addComponent(gameTitleLabel)
        			.addContainerGap(264, Short.MAX_VALUE))
        		.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
        			.addGap(109)
        			.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
        				.addComponent(addMembersLabel)
        				.addComponent(professionTextField)
        				.addComponent(enterNameLabel))
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addComponent(additionalMembersList, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE)
        				.addComponent(addButton, GroupLayout.PREFERRED_SIZE, 117, GroupLayout.PREFERRED_SIZE)
        				.addComponent(professionList, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE)
        				.addComponent(enterNameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addContainerGap(173, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addContainerGap()
        			.addComponent(gameTitleLabel, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
        			.addGap(18)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(enterNameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(enterNameLabel))
        			.addGap(10)
        			.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
        				.addComponent(addButton)
        				.addComponent(addMembersLabel))
        			.addGap(4)
        			.addComponent(additionalMembersList, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.UNRELATED)
        			.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(professionList, GroupLayout.PREFERRED_SIZE, 76, GroupLayout.PREFERRED_SIZE)
        				.addComponent(professionTextField))
        			.addGap(41)
        			.addComponent(nextButton)
        			.addContainerGap())
        );
        setLayout(groupLayout);
    }
}
